package com.jmodata.zxu.service.impl.manager;

import com.jmodata.zxu.dao.MyBatisCommonDAO;
import com.jmodata.zxu.model.DataGrid;
import com.jmodata.zxu.pojo.manager.LoginLog;
import com.jmodata.zxu.pojo.manager.Manager;
import com.jmodata.zxu.pojo.manager.ManagerRole;
import com.jmodata.zxu.service.manager.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("managerService")
@Transactional(rollbackFor=Exception.class)
public class ManagerServiceImpl implements ManagerService {

    @Autowired
    private MyBatisCommonDAO myBatisCommonDAO;


    @Override
    public DataGrid queryManagerList(Map<Object, Object> map) throws Exception {
        DataGrid dg = new DataGrid();
        List<Manager> list = new ArrayList<Manager>();
        list = myBatisCommonDAO.executeForObjectList("manager.queryManagerList", map);
        dg.setRows(list);
        dg.setTotal(myBatisCommonDAO.executeForObjectListCount("manager.queryManagerListCount", map));
        return dg;
    }

    @Override
    public Manager getManagerLogin(String username, String password) throws Exception {
        Manager manager = new Manager();
        manager.setUsername(username);
        manager.setPassword(password);
        manager = myBatisCommonDAO.executeForObject("manager.getManagerLogin", manager, Manager.class);
        if (null != manager) {
            return manager;
        }
        return null;
    }

    @Override
    public int updatePwd(Manager manager) throws Exception {
        int flag = 0;
        try {
            flag = myBatisCommonDAO.executeUpdate("manager.updatePwd", manager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public int insertManager(Manager manager) throws Exception {

        // 添加管理员
        int flag = myBatisCommonDAO.executeInsert("manager.insertManager", manager);

        // 获取自增ID
        // Long id = myBatisCommonDAO.executeForObject("manager.getInsertId", null, Long.class);
        //
        // // 插入分配角色信息
        // if (!XhrtStringUtils.isEmpty(manager.getRoleIds())) {
        // String[] roleIds = manager.getRoleIds().split(",");
        // for (String roleId : roleIds) {
        // ManagerRole managerRole = new ManagerRole();
        // managerRole.setRole_id(new BigDecimal(roleId));
        // managerRole.setManager_id(new BigDecimal(id));
        // int flag1 = myBatisCommonDAO.executeInsert("manager.insertManagerRole", managerRole);
        // if (flag1 == 0) {
        // throw new MyCheckException("角色赋权错误,请重新尝试!!!");
        // }
        // }
        // }
        return flag;
    }

    @Override
    public int updateManager(Manager manager) throws Exception {
        // 更新
        int flag = myBatisCommonDAO.executeInsert("manager.updateManager", manager);
        //
        // myBatisCommonDAO.executeDelete("manager.deleteManagerRole", manager.getManager_id());
        //
        // // 插入分配角色信息
        // if (!XhrtStringUtils.isEmpty(manager.getRoleIds())) {
        // String[] roleIds = manager.getRoleIds().split(",");
        // for (String roleId : roleIds) {
        // ManagerRole managerRole = new ManagerRole();
        // managerRole.setRole_id(new BigDecimal(roleId));
        // managerRole.setManager_id(manager.getManager_id());
        // int flag1 = myBatisCommonDAO.executeInsert("manager.insertManagerRole", managerRole);
        // if (flag1 == 0) {
        // throw new MyCheckException("角色赋权错误,请重新尝试!!!");
        // }
        // }
        // }
        return flag;
    }

    @Override
    public int deleteManager(Integer managerid) throws Exception {
        int flag = myBatisCommonDAO.executeUpdate("manager.deleteManager", managerid);
        return flag;
    }

    @Override
    public Manager getManager(Integer managerid) throws Exception {
        Manager manager = myBatisCommonDAO.executeForObject("manager.getManager", managerid, Manager.class);
        return manager;
    }

    @Override
    public int insertManagerRole(ManagerRole managerRole) throws Exception {
        int flag = myBatisCommonDAO.executeInsert("manager.insertManagerRole", managerRole);
        return flag;
    }

    @Override
    public int deleteManagerRole(Integer managerid) throws Exception {
        int flag = myBatisCommonDAO.executeDelete("manager.deleteManagerRole", managerid);
        return flag;
    }

    @Override
    public List<Manager> getNameList() {
        List<Manager> paList = myBatisCommonDAO.executeForObjectList("manager.getNameList", "");
        return paList;
    }

    @Override
    public List<Manager> getEmployes() {
        return myBatisCommonDAO.executeForObjectList("manager.getEmploysAll", "");
    }

    /**
     * 新增管理员时判断用户名或证件是否重复
     */
    @Override
    public List<Manager> findManagerForCheckRepeat(Map<Object, Object> map) throws Exception {
        List<Manager> list = myBatisCommonDAO.executeForObjectList("manager.findManagerForCheckRepeat", map);
        return list;
    }

    @Override
    public List<Manager> getEmploysAllNoRole() {
        return myBatisCommonDAO.executeForObjectList("manager.getEmploysAllNoRole", "");
    }

    @Override
    public List<Manager> getManagerByCommunityId(Map<Object, Object> map) throws Exception {
        return myBatisCommonDAO.executeForObjectList("manager.getManagerAllByCommunityId", map);
    }
}
